package com.yyfax.common.form;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import com.yyfax.common.R;
import com.yyfax.common.tools.DensityUtil;

/**
 * Created by fuqiang on 2017/5/4.
 * 带删除图标的EditText
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class ClearEditText extends AppCompatEditText implements View.OnFocusChangeListener, TextWatcher {
	private Drawable mClearDrawable;//清空按钮
	Drawable[] mDrawables = new Drawable[4];
	private OnFocusChangeListener mListener;

	/**
	 * 借款数额
	 */
	private int mAmount;

	/**
	 * 最大金额
	 */
	private int mRemain;


	public void setOnFocusChangeListener(OnFocusChangeListener listener) {
		this.mListener = listener;
	}

	public ClearEditText(Context context) {
		super(context);
	}

	public ClearEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public ClearEditText(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context, attrs);
	}

	protected void init(Context context, AttributeSet attrs) {
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FormRow);
		mDrawables = getCompoundDrawables();
		if (getPaddingRight() == 0) {
			setPadding(getPaddingLeft(), getPaddingTop(), DensityUtil.dip2px(context, 12), getPaddingBottom());
		}
		String hint = ta.getString(R.styleable.FormRow_hint);
		if (!TextUtils.isEmpty(hint))
			setHint(hint);//设置hintText

		Drawable drawable = ta.getDrawable(R.styleable.FormRow_drawableRight);
		if (drawable != null) {
			mClearDrawable = drawable;
		} else {
			mClearDrawable = context.getResources().getDrawable(R.drawable.input_del_flag_icon);
		}
		mClearDrawable.setBounds(0, 0, mClearDrawable.getMinimumWidth(), mClearDrawable.getMinimumHeight());

		ColorStateList rightTextColorHint = ta.getColorStateList(R.styleable.FormRow_textColorHint);//设置textcolorhint
		if (rightTextColorHint != null)
			setHintTextColor(rightTextColorHint);
		else
			setHintTextColor(getResources().getColor(R.color.color_cccccc));

		ColorStateList rightTextColor = ta.getColorStateList(R.styleable.FormRow_rightTextColor);//设置textcolor
		if (rightTextColor != null)
			setTextColor(rightTextColor);
		else
			setTextColor(getResources().getColor(R.color.color_000000));

		setFreezesText(ta.getBoolean(R.styleable.FormRow_freezesText, true));
		if (isSaveEnabled())
			setSaveEnabled(ta.getBoolean(R.styleable.FormRow_saveEnabled, false));

		int inputType = ta.getInt(R.styleable.FormRow_android_inputType, EditorInfo.TYPE_NULL);
		if (inputType != EditorInfo.TYPE_NULL)
			setInputType(inputType);//设置inputType

		int maxlength = ta.getInt(R.styleable.FormRow_maxLength, -1);//设置最大输入长度
		if (maxlength >= 0) {
			setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxlength)});
		}

		boolean rightTextBold = ta.getBoolean(R.styleable.FormRow_rightTextBold, false);
		getPaint().setFakeBoldText(rightTextBold);
		Drawable clearEditTextBackground = ta.getDrawable(R.styleable.FormRow_clearEditTextBackground);
		if (clearEditTextBackground != null) {
			setBackgroundDrawable(clearEditTextBackground);
		}else {
			setBackgroundDrawable(null);
		}
		setOnFocusChangeListener(this);
		addTextChangedListener(this);
		ta.recycle();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (getCompoundDrawables()[2] != null && event.getAction() == MotionEvent.ACTION_UP
				&& event.getX() > getWidth() - getTotalPaddingRight()
				&& event.getX() < getWidth() - getPaddingRight()) {
			getText().clear();
		}
		return super.onTouchEvent(event);
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		if (mListener != null) {
			mListener.onFocusChange(v, hasFocus);
		}
		if (hasFocus && getText().length() > 0) {//如果有焦点
			mDrawables[2] = mClearDrawable;//如果有内容, 显示清空按钮
		} else {//如果没有焦点
			mDrawables[2] = null;//不显示清空按钮
		}
		setCompoundDrawables(mDrawables[0], mDrawables[1], mDrawables[2], mDrawables[3]);
	}

	@Override
	public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {

	}

	@Override
	public void afterTextChanged(Editable s) {
		if (!TextUtils.isEmpty(getText()) && hasFocus()) {
			mDrawables[2] = mClearDrawable;
		} else {
			mDrawables[2] = null;
		}
		setCompoundDrawables(mDrawables[0], mDrawables[1], mDrawables[2], mDrawables[3]);
	}

	public void updateDrawables() {
		mDrawables = getCompoundDrawables();
		mDrawables[2] = mClearDrawable;
	}

	public interface OnFocusChangeListener {
		void onFocusChange(View v, boolean hasFocus);
	}

	public void parseAmount() {
		try {
			mAmount = Integer.parseInt(getText().toString());
		} catch (Exception e) {
			mAmount = 0;
		}
	}

	public int getmAmount() {
		return mAmount;
	}

	public void setRemain(int remain) {
		this.mRemain = remain;
	}

	public int getRemain() {
		return mRemain;
	}

	public String getFormText() {
		return getText().toString().trim();
	}

	public void addOnFocusChangeListener(OnFocusChangeListener listener) {

	}

}
